package com.gykj.push;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import cn.jpush.android.api.JPushInterface;

import static com.gykj.push.ExampleUtil.printBundle;


public class JPushActivity extends AppCompatActivity {
    private static String TAG = "推送中心";
    public static boolean isForeground = false;

    protected MessageReceiver mMessageReceiver;
    private PushListener pushListener;//自定义事件响应接口

    /**
     * 接收推送的控制中心
     */

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    public class MessageReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(getApplicationContext(), "收到消息", Toast.LENGTH_SHORT).show();

            try {
                Bundle bundle = intent.getExtras();
                if (EventType.EVENT_REGIST_ID.equals(intent.getAction())) {
                    //获取事件类型
                    String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
                    pushListener.onRegisterId(regId);
                } else if (EventType.EVENT_MESSAGE_RECEIVE.equals(intent.getAction())) {
                    pushListener.onMessageReceived(bundle.getString(JPushInterface.EXTRA_TITLE),
                            bundle.getString(JPushInterface.EXTRA_MESSAGE),
                            bundle.getString(JPushInterface.EXTRA_EXTRA),
                            bundle.getString(JPushInterface.EXTRA_MSG_ID));

                } else if (EventType.EVENT_NOTIFICATION_RECEIVE.equals(intent.getAction())) {
                    pushListener.onNotificationReceived(
                            bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE),
                            bundle.getString(JPushInterface.EXTRA_ALERT),
                            bundle.getString(JPushInterface.EXTRA_EXTRA),
                            bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID));
                } else if (EventType.EVENT_NOTIFICATION_OPEN.equals(intent.getAction())) {
                    pushListener.onNotificationOpened(
                            bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE),
                            bundle.getString(JPushInterface.EXTRA_ALERT),
                            bundle.getString(JPushInterface.EXTRA_EXTRA),
                            bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID));
                } else if (EventType.EVENT_NOTIFICATION_CLICK.equals(intent.getAction())) {
                    pushListener.onNotificationClicked(
                            bundle.getString(JPushInterface.EXTRA_NOTIFICATION_ACTION_EXTRA));
                } else if (EventType.EVENT_CONNECTION_CHANGE.equals(intent.getAction())) {
                    pushListener.onConnectionChanged(intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false));
                }else if(EventType.EVENT_UNDEFINED.equals(intent.getAction())){
                    pushListener.onUndefinedEvent(printBundle(bundle));
                }
            } catch (Exception e) {

            }
        }
    }



    /**
     * 设置响应推送监听
     *
     * @param pushListener
     */
    protected void setPushListener(PushListener pushListener) {
        this.pushListener = pushListener;
        Log.e(TAG, "注册服务");
        mMessageReceiver = new MessageReceiver();
        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(EventType.EVENT_REGIST_ID);
        filter.addAction(EventType.EVENT_MESSAGE_RECEIVE);
        filter.addAction(EventType.EVENT_NOTIFICATION_RECEIVE);
        filter.addAction(EventType.EVENT_NOTIFICATION_OPEN);
        filter.addAction(EventType.EVENT_NOTIFICATION_CLICK);
        filter.addAction(EventType.EVENT_CONNECTION_CHANGE);
        filter.addAction(EventType.EVENT_UNDEFINED);

        registerReceiver(mMessageReceiver, filter);
    }

    /**
     * 移除推送监听
     */
    protected void removePushListener() {
        unregisterReceiver(mMessageReceiver);
        this.pushListener = null;
    }

    /**
     * 请求权限
     * @test Android6.0以上的权限请求
     */
    private void grantPermission() {
        //请求本地文件读写权限，

        String[] permission = {
                Manifest.permission.INTERNET,
                Manifest.permission.CHANGE_NETWORK_STATE,
                Manifest.permission.GET_TASKS,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
                Manifest.permission.CHANGE_WIFI_STATE,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.SYSTEM_ALERT_WINDOW,
                Manifest.permission.VIBRATE,
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS,
                Manifest.permission.WRITE_SETTINGS,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE,
        };
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
                    ActivityCompat.checkSelfPermission(this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, permission, 1);
            } else {

            }
        } else {

        }
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(mMessageReceiver);
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        isForeground = true;
        super.onResume();
    }


    @Override
    protected void onPause() {
        isForeground = false;
        super.onPause();
    }
}
